package hu.veilingsite.gui;

import hu.veilingsite.client.Service;
import hu.veilingsite.client.ServiceAsync;
import hu.veilingsite.client.Systeem;
import hu.veilingsite.shared.ClientUser;
import hu.veilingsite.shared.FieldVerifier;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class Inloggen {
	private Label mail, wachtwoord,errorMsg;
	private TextBox email;
	private PasswordTextBox password;
	private Button login;

	public Inloggen(){
		final ServiceAsync myService = (ServiceAsync)
		GWT.create(Service.class);
		final AsyncCallback<ClientUser> getGebruiker = new AsyncCallback<ClientUser>(){

			@Override
			public void onFailure(Throwable caught) {
				FieldVerifier.showMessageBox("Oops", "Er is iets fout gegaan op de server..", true);
			}

			@Override
			public void onSuccess(ClientUser result) {
				Systeem.ingelogd = result;
				Systeem.createMenu();
				RootPanel.get("main").clear();
				RootPanel.get("main").add(new Profile().createPage());
				HorizontalPanel p = new HorizontalPanel();
				Label account = new Label("U bent: " + result.getName());
				p.add(account);
				RootPanel.get("accountinfo").add(p);
			}
		};
		
		final AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
			@Override
			public void onFailure(Throwable caught) {
				FieldVerifier.showMessageBox("Oops", "Er is iets fout gegaan op de server..", true);
			}
			@Override
			public void onSuccess(Boolean result) {
				if(result){
					myService.getUser(email.getText(), getGebruiker);
				}
				else{
					errorMsg.setText("De door u ingevulde gegevens zijn niet correct.");
				}
			}
		};
		errorMsg = new Label("");
		mail = new Label("E-mail adres:");
		wachtwoord = new Label("Wachtwoord:");
		email = new TextBox();
		password = new PasswordTextBox();
		login = new Button("Inloggen");

		login.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent e){
				myService.login(email.getText(), password.getText(), callback);
			}
		});
	}
	public Panel createPage(){
		VerticalPanel result = new VerticalPanel();
		
		HTML intro = new HTML();
		intro.setHTML("<h1>Inloggen</h1>Wanneer u een account heeft aangemaakt kunt u inloggen met het onderstaande formulier.<br />Heeft u zicht nog niet geregisteerd? Dan kan dat <a href='#registreer'>hier</a>.<br /><br />");
		result.add(intro);
		
		FlexTable table = new FlexTable();
		table.addStyleName("nicetable");
		
		table.setWidget(0, 0, mail);
		table.setWidget(0,  1, email);
		table.setWidget(1,  0, wachtwoord);
		table.setWidget(1,  1, password);
		table.setWidget(2, 0, errorMsg);
		table.setWidget(2,  1, login);
		result.add(table);
		
		return result;
	}
}
